//
//  SynchronizedDemo.m
//  JKThreadTest
//
//  Created by 王冲 on 2018/10/25.
//  Copyright © 2018年 JK科技有限公司. All rights reserved.
//

#import "SynchronizedDemo.h"

@implementation SynchronizedDemo


-(void)__buyTicket{
    
    static NSObject *object;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        object = [[NSObject alloc]init];
    });
    
    // () 里面当一个对象就好 相当于锁
    @synchronized (object) {
      
         [super __buyTicket];
    }
}

-(void)__drawMoney{
    
    @synchronized (self) {
        
        [super __drawMoney];
    }
    
}

-(void)__saveMoney{
    
    @synchronized (self) {
        
        [super __saveMoney];
    }

}

// 同一个线程可以对同一把锁进行加锁
- (void)otherTest
{

    @synchronized (self) {
        
        NSLog(@"%s",__func__);
        
        [self otherTest];
    }
}

@end
